package slu.appli.wodtimer.swing.adapter;

import javax.swing.SwingUtilities;

import slu.appli.tools.parser.stringparser.interfaces.model.GroupInterface;
import slu.appli.tools.parser.stringparser.interfaces.model.ModelReaderInterface;
import slu.appli.tools.parser.stringparser.interfaces.model.ModelReaderListenerInterface;

public abstract class ModelReaderSwingAdapter
implements ModelReaderListenerInterface {

	@Override
	public void readerStarted(ModelReaderInterface reader) {
		this.readerStartedForSwing(reader);
	}

	private void readerStartedForSwing(ModelReaderInterface reader) {
		if (SwingUtilities.isEventDispatchThread()) {
			this.readerStarted_Swing(reader);
		}
		else {
			SwingUtilities.invokeLater(new InnerReaderStartedSwingTask(reader));
		}
	}
	
	public abstract void readerStarted_Swing(ModelReaderInterface reader);
	
	private class InnerReaderStartedSwingTask implements Runnable {
		private final ModelReaderInterface reader;
		
		public InnerReaderStartedSwingTask(ModelReaderInterface reader) {
			super();
			this.reader = reader;
		}
		@Override
		public void run() {
			ModelReaderSwingAdapter.this.readerStarted_Swing(reader);
		}
		
	}

	@Override
	public void readerRestarted() {
		this.readerRestartedForSwing();
	}

	private void readerRestartedForSwing() {
		if (SwingUtilities.isEventDispatchThread()) {
			this.readerRestarted_Swing();
		}
		else {
			SwingUtilities.invokeLater(new InnerReaderRestartedSwingTask());
		}
	}
	
	public abstract void readerRestarted_Swing();
	
	private class InnerReaderRestartedSwingTask implements Runnable {
		@Override
		public void run() {
			ModelReaderSwingAdapter.this.readerRestarted_Swing();
		}
		
	}

	@Override
	public void groupReaded(GroupInterface group) {
		this.groupReadedForSwing();
	}

	private void groupReadedForSwing() {
		if (SwingUtilities.isEventDispatchThread()) {
			this.groupReaded_Swing();
		}
		else {
			SwingUtilities.invokeLater(new InnerGroupReadedSwingTask());
		}
	}
	
	public abstract void groupReaded_Swing();
	
	private class InnerGroupReadedSwingTask implements Runnable {
		@Override
		public void run() {
			ModelReaderSwingAdapter.this.groupReaded_Swing();
		}
		
	}

	@Override
	public void itemReaded(Object item) {
		this.itemReadedForSwing(item);
	}

	private void itemReadedForSwing(Object item) {
		if (SwingUtilities.isEventDispatchThread()) {
			this.itemReaded_Swing(item);
		}
		else {
			SwingUtilities.invokeLater(new InnerItemReadedSwingTask(item));
		}
	}
	
	public abstract void itemReaded_Swing(Object item);
	
	private class InnerItemReadedSwingTask implements Runnable {
		private final Object item;
		
		public InnerItemReadedSwingTask(Object item) {
			super();
			this.item = item;
		}
		@Override
		public void run() {
			ModelReaderSwingAdapter.this.itemReaded_Swing(item);
		}
		
	}

	@Override
	public void readerEnded() {
		this.readerEndedForSwing();
	}

	private void readerEndedForSwing() {
		if (SwingUtilities.isEventDispatchThread()) {
			this.readerEnded_Swing();
		}
		else {
			SwingUtilities.invokeLater(new InnerReaderEndedSwingTask());
		}
	}
	
	public abstract void readerEnded_Swing();
	
	private class InnerReaderEndedSwingTask implements Runnable {
		@Override
		public void run() {
			ModelReaderSwingAdapter.this.readerEnded_Swing();
		}
		
	}

	@Override
	public void stopPerformed() {
		this.stopPerformedForSwing();
	}

	private void stopPerformedForSwing() {
		if (SwingUtilities.isEventDispatchThread()) {
			this.stopPerformed_Swing();
		}
		else {
			SwingUtilities.invokeLater(new InnerStopPerformedSwingTask());
		}
	}
	
	public abstract void stopPerformed_Swing();
	
	private class InnerStopPerformedSwingTask implements Runnable {
		@Override
		public void run() {
			ModelReaderSwingAdapter.this.stopPerformed_Swing();
		}
		
	}

	@Override
	public void pausePerformed() {
		this.pausePerformedForSwing();
	}

	private void pausePerformedForSwing() {
		if (SwingUtilities.isEventDispatchThread()) {
			this.pausePerformed_Swing();
		}
		else {
			SwingUtilities.invokeLater(new InnerPausePerformedSwingTask());
		}
	}
	
	public abstract void pausePerformed_Swing();
	
	private class InnerPausePerformedSwingTask implements Runnable {
		@Override
		public void run() {
			ModelReaderSwingAdapter.this.pausePerformed_Swing();
		}
		
	}

}
